<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
		/*修改或者是添加   add  edit  */
		String states =new String(request.getParameter("states").getBytes("ISO-8859-1"),"utf-8");
		/* 功能类型状态  是哪个功能 */
		String ckType =request.getParameter("ckType");
		/* 功能名字 */
		String typeName =new String(request.getParameter("typeName").getBytes("ISO-8859-1"),"utf-8");
		String dataId =request.getParameter("rowid") ;
		
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";

	String pic_basePath = "http://zgaf.org:8080/"; // 图片上传路径
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <title>Insert title here</title>
</head>
<script src="<%=basePath %>them/ckeditor/ckeditor.js"></script>
<jsp:include page="public/public.jsp"/>

<link rel="stylesheet" type="text/css" href="home/public/css/style.css" />
<link rel="stylesheet" type="text/css" href="home/public/css/bases.css" />
<!--上传  -->
<script type="text/javascript" src="<%=basePath%>them/jquery-easyui-1.4.2/ajaxfileupload.js"></script>
<script type="text/javascript">

CKEDITOR.editorConfig = function( config ) {
	config.toolbar =
		[
		{ name: 'document', items: ['Source', '-', 'Preview'] },
		{ name: 'clipboard', items: ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'] },
		{ name: 'styles', items: ['Styles', 'Format', 'Font', 'FontSize'] },
		{ name: 'colors', items: ['TextColor', 'BGColor'] },
		{ name: 'tools', items: ['About'] },
		'/',
		{ name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'] },
		{ name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl'] },
		{ name: 'links', items: ['Link', 'Unlink', 'Anchor'] },
		{ name: 'insert', items: ['Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar', 'PageBreak',] }
		
	];
	config.filebrowserImageUploadUrl = '<%=basePath%>upload/CKImgUpload';
	config.height=500;
};
</script>

<script type="text/javascript">
/* 根据id查询  修改时   已进入页面就进行此操作 */
function queryMsg(id){
	$.ajax({  
		url:'<%=basePath %>ck/queryHtmlById',
		type:'post',
		data:{
		"id":id
		},
		dataType:'json',
		error:function(){
			$.messager.alert("提示","查询"+typeName+"异常","error");
		}, 
		success:function(map){ 
			var data =map.entity;
			var title = data.title;
			var summary =data.summary;
			var html = data.html
	   		$("#titleId").textbox("setValue",title);
	   		$("#summaryId").textbox("setValue",summary);
	   		var img = data.img;
	   		if(img !=null && img !='null' && img !=''){
				$("#menuimgId").val(img);
		   		img = "<%=pic_basePath%>images/uploadimg/"+img;
		   		$("#yuimgId").attr("src",img);
		   		/* $("#yuimgId").css("display","block"); */
			}


            $("#htmlId").val(html);

	 		$("#hideUrlId").val(data.uploadurl);
	 		if(data.uploadurl !=null && data.uploadurl !=''){
					  var urlStr = data.uploadurl;
					  var urlsplit = urlStr.split(",");
					  for(var i = 1;i<urlsplit.length;i++){
						  if(urlsplit[i]!=null && urlsplit[i]!=''){
							  var urlAndName = urlsplit[i].split("(&split&)");
							  var id=urlAndName[0].split(".");
							  var downUrl = '../down/'+urlAndName[0];

							  var strdown = '<p id="'+id[0]+'"><a  target="_Blank"  class="textloding" href="'
								  +downUrl
								  +'">'
								  +urlAndName[1]
								  +'</a><input id="submit3" type="button" onclick="delfile('+"'"+urlAndName[0]+"'"+','+"'"+urlAndName[1]+"'"+')" value="删 除"></p>';
							  $("#fileloadId").append(strdown);
						  }
					  }
			 }
		}  
	});
}
$(function(){
	var states = '<%=states %>';
	var typeName='<%=typeName %>';
	if(typeName!=null && typeName!=''){
		typeName = decodeURI(decodeURI(typeName));
	}
	var dataId = <%=dataId %>;
	
	if(states=='add'){
		$("#states").html("添加"+typeName);
		$("#subButton").val("完 成 添 加");
	}
	if(states=='edit'){
		$("#states").html("修改"+typeName);
		$("#subButton").val("完 成 修 改");
		queryMsg(dataId);	
	}
	$("#typeName").html(typeName);
})
/* 全局变量 start */
 
  	  /* var imgHeight;
	  var imgWidth;
	  var imgSize; */
	  /* if(type==2){ */
		  //动态
		 /*  imgWidth = 127;
		  imgHeight = 105;
		  imgSize = 200; */
	  /* } */
 
 /* end */
//图片上传
 function fn_img_upload(){
	  var url = '<%=basePath%>upload/fileUpload';
	  var suffixStr = "jpg,JPG,png,PNG,gif,GIF,bmp,BMP";
	  var savePath = ",images,uploadimg";/* 图片存储路径 */
	  $.ajaxFileUpload({//String fileObjectId,int MaxSize,String suffixStr,String savePath,int width,int height
           url: url+'?fileObjectId=topImg&MaxSize='+0+'&suffixStr='+suffixStr+'&savePath='+savePath+'&width='+0+'&height='+0 + "&timestamp=" + new Date().getTime(), //用于文件上传的服务器端请求地址
           secureuri: false, //是否需要安全协议，一般设置为false
           fileElementId: 'imgId', //文件上传域的ID
           dataType: 'json', //返回值类型 一般设置为json
           success: function (data, status)  //服务器成功响应处理函数
           {
               if(data.success=="true"){
            	   $("#menuimgId").val(data.imgurl);
            	  
             	   $("#yuimgId").attr("src","<%=pic_basePath%>images/uploadimg/"+data.imgurl);
             	   $("#yuimgId").val(data.imgurl);
            	   $("#yuimgId").css("display","block");
               }else{
            	   $.messager.alert("提示",data.msg,"error");
             		return;
               }
           },
           error: function (data, status, e)//服务器响应失败处理函数
           {
        	   $("#imgId").val("");
        	   $("#menuimgId").val("");
        	   $.messager.alert("提示",e,"error");
				return;
           }
    })
} 
function fn_uploadinit(){
	fn_upload("myhead","hideUrlId");
}

//附件上传
function fn_upload(fileObjectId,hideUrlId){
	 var hideUrlVal = $("#"+hideUrlId).val();
	 
	 var fileurl = hideUrlVal.split(",");
	  var url = '../upload/fileUpload';
	  var suffixStr = "jpg,png,gif,pdf,zip,rar,xls,xlsx,docx,doc";
	  var savePath = ",down";/* 文件存储路径 */
	  $.ajaxFileUpload({
          url: url+'?fileObjectId='+fileObjectId+'&MaxSize='+0+'&suffixStr='+suffixStr+'&savePath='+savePath+'&width='+0+'&height='+0 + "&timestamp=" + new Date().getTime(), //用于文件上传的服务器端请求地址
          secureuri: false, //是否需要安全协议，一般设置为false
          fileElementId: fileObjectId, //文件上传域的ID
          dataType: 'json', //返回值类型 一般设置为json
          success: function (data, status)  //服务器成功响应处理函数
          {
              if(data.success=="true"){
            	  var oldval = $("#"+hideUrlId).val();
            	  var pid = data.imgurl.split(".");
            	  
            	  $("#"+hideUrlId).val(oldval+","+data.imgurl+"(&split&)"+data.oldname);
            	  var fileloadStr='<p class="text_indent_120 pfileloding" id="'+pid[0]+'">'
            		  	+data.oldname
						+'<input id="submit3" type="button" onclick="delfile('+"'"+data.imgurl+"'"+','+"'"+data.oldname+"'"+')" value="删 除"></p>';
            	 
            	  $("#fileloadId").append(fileloadStr);
            	  alert("附件上传成功!");
            	  
            	
              }else{
           	   	  alert(data.msg);
            		return;
              }
          },
          error: function (data, status, e)//服务器响应失败处理函数
          {
       	   		alert(e);
				return;
          }
   })
} 
//删除文件
function delfile(url,oldname){
	 if(confirm("确定删除此附件吗?")){
		 var pid = url.split(".");
		 $("#"+pid).remove();
		 
		 var hideUrlVal = $("#hideUrlId").val();
		 var delurl = ","+url+"(&split&)"+oldname;
		 hideUrlVal=hideUrlVal.replace(delurl, "");
		 $("#hideUrlId").val(hideUrlVal);
		 var states = '<%=states %>';
		 
		if(states=='add'){
			del(delurl,url);
		}else {
			$("#hideUrl").val(url);
		}
		 
		 
	 }
	
}

function del(delurl,url){

	 $.ajax({
			  type: "post",
			  url: "../../xyService/delFile",
			  data: {
				  'fileurlStr':delurl,
				  'fileurl':url
			  },
			  dataType: "json",
			  success: function(data){
				  
			  }
	})
}

	  /* 提交功能 */
	  function subForm(){
		  var dataId = <%=dataId %>;
		  var html =CKEDITOR.instances.htmlId.getData();/* 富文本内容 */
		  var ckType = <%=ckType %>;/* 功能类型 */
		  var img ="";
		  var title =$("#titleId").val().replace(/\s/g, "");/* 标题 */
		  var summary = $("#summaryId").val().replace(/\s/g, "");/* 来源 */
		  var uploadurl = $("#hideUrlId").val().replace(/\s/g, "");/*附件值*/
		  var delurl=$("#hideUrl").val().replace(/\s/g, "");
		  if(title==""){
			  $.messager.alert("提示","标题不允许为空","error");
			  return;
		  }
		  if(summary==""){
			  $.messager.alert("提示","来源不允许为空","error");
			  return;
		  }
		  img = $("#menuimgId").val().replace(/\s/g, "");/* 首页图片 */
		  if(ckType==24||ckType==25){
			
			  if(img==""){
				  $.messager.alert("提示","图标不允许为空","error");
				  return;
			  }
			  
		  }else{
			  if(html==""){
				  $.messager.alert("提示","内容不允许为空","error");
				  return;
			  }
		  }
		  
		 
		  var htmlUrl="";
		  var addUpdateNames="";
		  var states = '<%=states %>';
			if(states=='add'){
				htmlUrl = '<%=basePath %>ck/addCkHtml';
				addUpdateNames="添加";
			}
			if(states=='edit'){
				htmlUrl = '<%=basePath %>ck/updateCkHtml';
				addUpdateNames="修改";
			}
			var typeNames='<%=typeName %>';
			if(typeNames!=null && typeNames!=''){
				typeNames = decodeURI(decodeURI(typeNames));
			}
		  $.ajax({  
				url:htmlUrl,
				type:'post',
				data:{
				"id":dataId,
				"html":html,
				"ckType":ckType,
				"img":img,
				"title":title,
				"summary":summary,
				"uploadurl":uploadurl
				},
				dataType:'json',
				error:function(){
					$.messager.alert("提示",addUpdateNames+typeNames+"异常","error");
				}, 
				success:function(map){
				if(delurl!=null&&delurl!=''){
					del('',delurl);
				}
					var row = map.row;
					if(row>=0){
						$.messager.confirm('确认',addUpdateNames+typeNames+'成功',function(r){       
						    	window.returnValue = "success";
								window.close();
								return; 
						});
					}else{
						$.messager.alert("提示",addUpdateNames+typeNames+"失败","error");
						return;
					}
				}  
			});  
	  }
</script>
<body>

  <div class="wi" id="center" align="center"> 
 		 <div class="con" style="border: 1px solid #cccccc;margin-top: 15px;width: 70%">
 		
  		
  			<table id="codetable" style="width:795px; padding-left: 0px;color: gray;margin-left: 5px;">
  				<tr  height="40px;">
  					<td colspan="2" align="center">
  					<span id="states" style="color: #ed5736;font-size: 30px"></span>
  					</td>
  				</tr>
  				<tr  height="40px;">
  					<td style="font-weight:bold;width: 100px;"align="right">类型:</td>
  					<td >
  					<span id="typeName" style="color: #ed5736"></span>
  					</td>
  				</tr>
  				<tr  height="30px;">
  					<td style="font-weight:bold;width: 100px;" align="right"><span style="color: red">*</span><span id="titleShowId">标题</span>:</td>
  					<td>
  					
  						<input class="easyui-textbox" style="color: #ed5736" id="titleId"/>
  						&nbsp;&nbsp;<span style="color: red" id="titlemsgId">标题为必填项</span>
  					</td>
  				</tr>
  				<tr  height="30px;">
  					<td style="font-weight:bold;width: 100px;" align="right">
  				<span style="color: red">*</span><span id="showMsgId">来 源</span>:
  					</td>
  					<td>
  						<input  class="easyui-textbox" id="summaryId"/>
  					</td>
  				</tr>
  				<!-- 图片上传 -->
  				<tr id="imgshowId" height="30px;" class="imageMsg">
  					<td style="font-weight:bold;width: 100px;" align="right">
  					
  					上传图片:
  					</td>
  					<td>
  						<input id="imgId" name="topImg" type="file" onchange="fn_img_upload()"/> 
  						<span id="imgMsg" style="color: red">
  							
  						</span>
	    				<input type="hidden" id="menuimgId"/>
  					</td>
  				</tr>
  				<tr id="yulan" class="imageMsg">
  					<td style="font-weight:bold;width: 100px;" align="right">图片预览:</td>
  					<td>
  						<img id="yuimgId" src="<%=basePath %>images/sys/noimage.jpg" width="110px" height="110px">
  					</td>
  				</tr>
  				<!-- end -->
  				<tr  height="300px;" id="htmlTrId">
  					<td style="font-weight:bold;width: 100px;" align="right">
  					<span style="color: red">*</span>
  					内容:
  					</td>
  					<td>
  						<textarea class="ckeditor" id="htmlId"></textarea>
  					
  					</td>
  				</tr>
  				<tr >
  				<td style="font-weight:bold;width: 100px;" align="right">
  				附件上传:</td>
  				<td >
                     <input name="myhead" id="myhead" onchange="fn_uploadinit()" type="file" multiple="multiple" class="select1" />
                     <input id="hideUrlId" style="display:none;"/> 
                     <input id="hideUrl" style="display:none;" />
                     <p id="fileloadId"></p>
  				</td>
  				
  				</tr>
  				<tr  height="50px;">
  					<td colspan="2" align="center">
  					<input id="subButton"  onclick="subForm()"
  					style="background-color: #ed5736;width: 200px;height: 50px;border: 0px;font-size: 22px;color: white;border-radius: 30px;" 
  					type="button" />
  					</td>
  				</tr>
  				<tbody id="codebody"></tbody>
  			</table>
  		</div> 
     </div> 
     <script type="text/javascript">
     var ckType  = <%=ckType %>;
		if(ckType==24||ckType==25){
			$("#htmlTrId").hide();
			$("#yuimgId").attr("width","40px");
			$("#yuimgId").attr("height","40px");
			$("#imgMsg").html("建议:图标宽度:40,图标高度:40");
			$("#titleShowId").html("联系方式");
			$("#titlemsgId").html("联系方式为必填项");
			$("#showMsgId").html("联系内容");
		}
     </script>
</body>
</html>